
import java.util.Scanner;

public class Main {

    private static String ram[] = new String[1000];
    private static int reg[] = new int[10];
    private static int casos, instruccion, cantInstrucciones, puntero;
    private static Scanner consola = new Scanner(System.in);
    private static StringBuffer s = new StringBuffer();

    public static void main(String args[]) {

        casos = consola.nextInt();
        for (int i = 0; i < casos; i++) {
            consola.nextLine();
            consola.nextLine();

            //Cargar RAM
            for (int j = 0; j < ram.length; j++) {
                ram[j] = consola.nextLine();
                if (ram[j].compareTo("100") == 0) {
                    break;
                }
            }

            //Ejecucion
            puntero = 0;
            instruccion = Integer.parseInt(ram[puntero]);
            cantInstrucciones = 1;
            while (instruccion != 100) {
                char i0;
                int i1, i2, iii=78/100;
                char[] c = ram[puntero].toCharArray();
                i0 = c[0];
                i1 = Integer.parseInt(String.valueOf(c[1]));
                i2 = Integer.parseInt(String.valueOf(c[2]));

                switch (i0) {
                    //0ds means goto the location in register d unless register s contains 0
                    case '0':
                        if (i2 != 0) {
                            puntero = reg[i1];
                        } else {
                            puntero++;
                        }
                        break;
                    case '1':
                        break;
                    //2dn means set register d to n (between 0 and 9)
                    case '2':
                        reg[i1] = i2;
                        puntero++;
                        break;
                    //3dn means add n to register d
                    case '3':
                        reg[i1] += i2;
                        reg[i1] %= 1000;
                        puntero++;
                        break;
                    //4dn means multiply register d by n
                    case '4':
                        reg[i1] *= i2;
                        reg[i1] %= 1000;
                        puntero++;
                        break;
                    //5ds means set register d to the value of register s
                    case '5':
                        reg[i1] = reg[i2];
                        puntero++;
                        break;
                    //6ds means add the value of register s to register d
                    case '6':
                        reg[i1] += reg[i2];
                        reg[i1] %= 1000;
                        puntero++;
                        break;
                    //7ds means multiply register d by the value of register s
                    case '7':
                        reg[i1] *= reg[i2];
                        reg[i1] %= 1000;
                        puntero++;
                        break;
                    //8da means set register d to the value in RAM whose address is in register a
                    case '8':
                        reg[i1] = Integer.parseInt(ram[reg[i2]]);
                        puntero++;
                        break;
                    //9sa means set the value in RAM whose address is in register a to the value of register s
                    case '9':
                        ram[reg[i2]] = String.valueOf(reg[i1]);
                        puntero++;
                        break;
                }
                instruccion = Integer.parseInt(ram[puntero]);
                cantInstrucciones++;
            }//Fin ejecucion
            s.append(cantInstrucciones).append(" ");

        }//Fin del caso
        System.out.print(s);
    }//FIN MAIN
}
